package com.cxg.fairy.ec.main.personal.message;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.ui.recycler.DataConverter;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by CFairy
 */

public class MessageCenterFundsDataConverter extends DataConverter {

    @Override
    public ArrayList<MultipleItemEntity> convert() {
        DateFormat dateFormat =  new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
        final JSONArray array = JSON.parseObject(getJsonData()).getJSONArray("data");
        int itemType = JSON.parseObject(getJsonData()).getInteger("itemType");
        final int size = array.size();
        for (int i = 0; i < size; i++) {

            final JSONObject data = array.getJSONObject(i);
            final int id = data.getInteger("mcMessageId");
            final String title = data.getString("title");
            final String content = data.getString("des");
            final long milliSeconds = data.getLong("createDate");

            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setItemType(MessageItemType.MESSAGE_CENTER_TYPE)
                    .setField(MultipleFields.ID, id)
                    .setField(MultipleFields.NAME, title)
                    .setField(MultipleFields.TAG, content)
                    .setField(MultipleFields.ITEM_TYPE,itemType)
                    .setField(MultipleFields.TEXT, dateFormat.format(new Date(milliSeconds)))
                    .build();
            ENTITIES.add(entity);
        }

        return ENTITIES;
    }

    @Override
    public MultipleItemEntity convertHeader() {
        return null;
    }
}
